#include<stdio.h>
#include"tea.h"
TeaNode *head;


/*
** 功能：创建奶茶节点
** id		奶茶唯一ID
** name		奶茶名称
** price	价格
*/
TeaNode *tea_create_node(int id,const char *name, int price)
{
	TeaNode *new = (TeaNode *)malloc(sizeof(TeaNode));
	if(new == NULL)
	{
		perror("内存分配失败\n");
		exit(1);
	}
	strncpy(new->name, name, sizeof(new->name));
	new->id = id;
	new->price = price;
	new->prev = new;
	new->next = new;
	
	return new;
}

/*
** 功能：插入奶茶节点
** head		奶茶链表
** new		准备添加的奶茶节点
*/ 
void list_add_tail(TeaNode *head,TeaNode *abc)
{
	abc->next = head;
	abc->prev = head->prev;
	
	head->prev->next = abc;
	head->prev = abc;
}

/*
** 功能：根据id删除奶茶节点
** head		奶茶链表
** new		准备添加的奶茶节点
*/ 
void delete_by_id(TeaNode *head, int id)
{
	TeaNode *tmp = head->next;
	
	for( ; tmp != head; tmp = tmp->next)
	{
		if( tmp->id == id )
		{
			tmp->prev->next = tmp->next;
			tmp->next->prev = tmp->prev;
			free(tmp);
			break;
		}
	}
	
	return ;
}

/*
** 功能：根据id查找货物节点
** head		货物链表
** new		准备查找的货物节点
*/ 
TeaNode *find_by_id(TeaNode  *head, int id)
{
	TeaNode  *tmp = head->next;
	
	for( ; tmp != head; tmp = tmp->next)
	{
		if( tmp->id == id )
		{
			return tmp;
		}
	}
	
	return NULL;
}

/*
** 功能：遍历货物
** head		货物链表
*/ 
void list_for_each(TeaNode *head)
{
	lv_main_demo();
	TeaNode *tmp = head->next;
	lv_textarea_set_text(tea_text, "用户列表：\n");
	char taitou[256] = {};
	snprintf(taitou,sizeof(taitou),"奶茶编号%-30s奶茶名称%-30s奶茶价格/元","","");
	lv_textarea_add_text(tea_text, taitou);
	lv_textarea_add_text(tea_text, "\n---------------------------------------------------------------------\n");
	for( ; tmp != head; tmp = tmp->next)
	{
		char line[128] = {};
		snprintf(line,sizeof(line),"%-50d\t%-40s\t%-8d\n",tmp->id, tmp->name, tmp->price);
		lv_textarea_add_text(tea_text,line);
	}
	
}

//根据修改奶茶的价格，名字
void chmod_tea_node(TeaNode *head,int id,char *name,int price)
{
	TeaNode *tmp;
	if ( (tmp = find_by_id(head,id))!=NULL)
	{
		strncpy(tmp->name,name,sizeof(name));

		tmp->price = price;
	}
	else
	{
		perror("failed to find the tea id");
		return;
	}
}

/*
** 功能：从文件加载数据
** filename		文件名字
*/ 
void file_load_data(TeaNode *head, const char *filename)
{
	FILE *fp = fopen(filename, "rb");
	if(fp == NULL)
	{
		perror("file_load_data fail");
		return ;
	}

	// 从文件读取数据，加载到链表
	TeaNode tmp; 
	while( fread(&tmp, sizeof(TeaNode), 1, fp) == 1 )
	{
		TeaNode *new = tea_create_node(tmp.id, tmp.name, tmp.price);
		list_add_tail(head, new);
	}
	
	fclose(fp);
}

/*
** 功能：保存数据到文件
** filename		文件名字
*/ 
void file_save_data(TeaNode *head, const char *filename)
{
	FILE *fp = fopen(filename, "wb");
	if(fp == NULL)
	{
		perror("file_save_data fail");
		return ;
	}
	
	// 把链表数据遍历，写入文件
	TeaNode *tmp = head->next;
	
	for( ; tmp != head; tmp = tmp->next)
	{
		fwrite(tmp, sizeof(TeaNode), 1, fp);
	}	
	
	fclose(fp);
}



void tea_main(void)
{

	FILE *fp = fopen("tea_info.txt","rb");
	head = tea_create_node(0,"",0);
	if (fp == NULL)
	{
		
		TeaNode *new1 = tea_create_node(1,"杨枝甘露",19);
		TeaNode *new2 = tea_create_node(2,"葡萄星空",18);
		TeaNode *new3 = tea_create_node(3,"芝士茉莉",11);
		TeaNode *new4 = tea_create_node(4,"布蕾脆脆奶芙",19);
		TeaNode *new5 = tea_create_node(5,"珍珠奶茶",12);
		TeaNode *new6 = tea_create_node(6,"曲香茉莉",9);
		TeaNode *new7 = tea_create_node(7,"大橘美式",19);
		TeaNode *new8 = tea_create_node(8,"苹果金凤梨",18);
		TeaNode *new9 = tea_create_node(9,"云顶抹茶龙井",18);
		list_add_tail(head,new1);
		list_add_tail(head,new2);
		list_add_tail(head,new3);
		list_add_tail(head,new4);
		list_add_tail(head,new5);
		list_add_tail(head,new6);
		list_add_tail(head,new7);
		list_add_tail(head,new8);
		list_add_tail(head,new9);
		file_load_data(head,"tea_info.txt");
	}
	else
		file_load_data(head,"tea_info.txt");
    


        
	
	// file_save_data(head,"tea_info.txt");
	// file_load_data(head,"tea_info.txt");
    

}
